package com.jy.wms.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.annotions.BaseServiceDescribe;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.common.base.SucessException;
import com.jy.wms.pojo.*;
import com.jy.wms.dao.WmsQualityTestingDetailedMapper;
import net.sf.json.JSONArray;
import org.apache.ibatis.javassist.runtime.Desc;
import org.springframework.stereotype.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import java.util.List;

/**
 * @ClassName: WmsQualityTestingDetailedService.java
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author LH
 * @Date Fri Nov 03 15:50:34 GMT+08:00 2017
 */

@Service
public class WmsQualityTestingDetailedService extends BaseService<WmsQualityTestingDetailedMapper, WmsQualityTestingDetailed> {

    /**
     * 插入一条记录
     *
     * @param entity 实体对象
     * @return int
     */
    public Integer baseInsert(List<WmsQualityTestingDetailed> entity) {
        return dao.baseInsert(entity);
    }

    /**
     * 插入一条记录（匹配有值的字段）
     *
     * @param entity 实体对象
     * @return int
     */
    @BaseServiceDescribe(moduleName = "质检规则", btnName = "增加规则明细")
    public Integer baseInsertSelective(WmsQualityTestingDetailed entity) throws SucessException, ServiceException {
        Integer a = dao.selectCount(new EntityWrapper<WmsQualityTestingDetailed>().eq("sx", entity.getSx()).eq("sszjgzid", entity.getSszjgzid()));
        if (a > 0) {
            throw new ServiceException("排序重复，请刷新页面重新增加");
        } else {
            if(entity.getBfb()!=null && entity.getBfb()!="") {//百分比抽检
                entity.setBfb(String.valueOf(Double.parseDouble(entity.getBfb()) / 100));
            }
            Integer b = dao.baseInsertSelective(entity);

            if (b > 0) {
                throw new SucessException("规则明细增加成功");
            } else {
                throw new ServiceException("规则明细增加失败");
            }
        }
    }

    /**
     * 删除（根据主键ID删除）
     *
     * @param id 主键
     * @return int
     */
    public Integer baseDeleteByPrimaryKey(String id) {
        return dao.baseDeleteByPrimaryKey(id);
    }

    /**
     * 删除（根据ID 批量删除）
     *
     * @param idList 主键ID列表
     * @return int
     */
    public Integer baseDeleteBathPrimaryKeys(List<String> idList) {
        return dao.baseDeleteBathPrimaryKeys(idList);
    }

    /**
     * 分页查询
     *
     * @param entity 实体
     * @return Page<WmsQualityTestingDetailed>
     */
    public Page<WmsQualityTestingDetailed> baseSelectPage(WmsQualityTestingDetailed entity) {
        PageHelper.startPage(entity.getPage(), entity.getRows());
        return (Page<WmsQualityTestingDetailed>) dao.baseSelectPage(entity);
    }

    /**
     * 查询（根据主键ID查询）
     *
     * @param id 主键ID
     * @return WmsQualityTestingDetailed
     */
    public WmsQualityTestingDetailed baseSelectByPrimaryKey(String id) {
        return dao.baseSelectByPrimaryKey(id);
    }

    /**
     * 查询（根据ID 批量查询）
     *
     * @param idList 主键ID列表
     * @return list
     */
    public Integer baseSelectBathPrimaryKeys(List<WmsQualityTestingDetailed> idList) {
        return dao.baseSelectBathPrimaryKeys(idList);
    }

    /**
     * 修改 （匹配有值的字段）
     *
     * @param entity 主键ID列表
     * @return int
     */
    public Integer baseUpdateByPrimaryKeySelective(WmsQualityTestingDetailed entity) {
        return dao.baseUpdateByPrimaryKeySelective(entity);
    }

    /**
     * 修改（根据主键ID修改）
     *
     * @param entity 主键ID列表
     * @return int
     */
    public Integer baseUpdateByPrimaryKey(WmsQualityTestingDetailed entity) {
        return dao.baseUpdateByPrimaryKey(entity);
    }

    @BaseServiceDescribe(moduleName = "质检规则", btnName = "查询明细集合")
    public JSONArray getDetailList(String sszjgzid) throws ServiceException {
        try {
            List<WmsQualityTestingDetailed> list = dao.getDetailedListBySszjgzid(sszjgzid);
            return JSONArray.fromObject(list);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("查询质检规则", e);
        }
    }

    /**
     * 删除质检规则明细 （匹配有值的字段）
     *
     * @param idList   明细id集合
     * @param sszjgzid 订单行
     * @return int
     */
    @BaseServiceDescribe(moduleName = "质检规则明细", btnName = "删除质检规则明细")
    public Integer baseDeleteByPrimaryKey(List<String> idList, String sszjgzid) throws SucessException, ServiceException {
        //查询当前质检下的规则明细数量，如果只剩下一条明细那么不可以删除
        Integer a = dao.selectCount(new EntityWrapper<WmsQualityTestingDetailed>().eq("sszjgzid", sszjgzid));
        if (a == 1) {
            throw new ServiceException("此订单只包含一条明细，不可以删除");
        } else {
            Integer count = dao.deleteBatchIds(idList);
            if (count > 0) {
                throw new SucessException("明细删除成功");
            } else {
                throw new ServiceException("明细删除失败");
            }
        }
    }

}
